package com.gec.zhs.interceptor;

import com.gec.zhs.component.RedisComponent;
import com.gec.zhs.controller.AGlobalExceptionHandlerController;
import com.gec.zhs.entity.constants.Constants;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.enums.ResponseCodeEnum;
import com.gec.zhs.exception.BusinessException;
import com.gec.zhs.utils.StringTools;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-01-25
 * @description
 */
@Component
public class AppInterceptor implements HandlerInterceptor {
    private static final String URL_ACCOUNT = "/account";
    private static final String URL_SWAGGER = "/swagger";
    private static final String URL_DOC = "/v2";
    private static final String URL_FILE = "/file";

    @Autowired
    private RedisComponent redisComponent;

    //登陆校验
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(request.getRequestURI());
        if (handler == null) {
            return false;
        }
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        if (request.getRequestURI().contains(URL_ACCOUNT) || request.getRequestURI().contains(URL_DOC)||request.getRequestURI().contains(URL_SWAGGER)) {
            return true;
        }
        String token = request.getHeader(Constants.TOKEN_ADMIN);

        if (request.getRequestURI().contains(URL_FILE)) {
            token = getToken(request);
        }
        if (StringTools.isEmpty(token)) {
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }
        //用户信息
        String tokenInfoForAdmin = redisComponent.getTokenInfoForAdmin(token);
        if (tokenInfoForAdmin == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }

        return true;
    }

    private String getToken(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            String token = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(Constants.TOKEN_ADMIN)) {
                    token = cookie.getValue();
                }
            }
            return token;
        }
        return null;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
